package com.ttl.bc.ap.purchaseorder.service;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Calendar;
import java.util.List;


import com.ttl.bc.ap.invoice.bo.BCAPInvoiceDAO;
import com.ttl.bc.ap.purchaseorder.bean.BCPurchaseOrderBean;


public class BCPurchaseOrderService {
	
	@SuppressWarnings("unchecked")
	public BCPurchaseOrderSummary getBCPurchaseOrder(String docNo) {
		
		BCPurchaseOrderSummary bean = new BCPurchaseOrderSummary();
		
		Calendar cal = Calendar.getInstance();
		
		String hql = "FROM BCPurchaseOrderBean WHERE (docNo = '"+docNo.trim()+"' OR ittl_purchaseCode = '"+docNo.trim()+"') " +
				"and poStatus!=2 " +
				"and (year(docDate)='"+cal.get(Calendar.YEAR)+"' or year(docDate)='"+(cal.get(Calendar.YEAR)-543)+"'  " +
						"or year(docDate)='"+(cal.get(Calendar.YEAR)-1)+"' or year(docDate)='"+(cal.get(Calendar.YEAR)-543-1)+"' )";
		
		BCAPInvoiceDAO dao = new BCAPInvoiceDAO();				
		List result = dao.find(hql);
		System.out.println(hql);
		if(result!=null && result.size()>0) {
			BCPurchaseOrderBean i = (BCPurchaseOrderBean)result.get(0);
			bean.setIttl_purchaseCode(i.getIttl_purchaseCode());
			bean.setDocNo(i.getDocNo());
			bean.setItems(i.getBcPurchaseOrderSubs().size());
			System.out.println(hql);
		}
		
		return bean;
	}

	@SuppressWarnings("unchecked")
	public BCPurchaseOrderSummary getMaxDocNoBCPurchaseOrder2(String docDateStr,boolean dyeChem) {
		
		String[] docDateStr2 = docDateStr.split("-");
		
		BCPurchaseOrderSummary bean = new BCPurchaseOrderSummary();
//		Calendar cal = Calendar.getInstance();
		String hql = "SELECT max(docNo) FROM BCPurchaseOrderBean WHERE ";
		if(dyeChem==true) {
			hql += " docNo like 'PO1-DC-%' ";
		}else {
			hql += " docNo not like '%DC%' and docNo like 'PO1-%' ";
		}
//		System.out.println("################");
//		CommonUtils
		hql += " and year(docDate)='"+docDateStr2[0]+"' ";
		hql += " and month(docDate)='"+docDateStr2[1]+"' ";
//		hql += " and (year(docDate)='"+cal.get(Calendar.YEAR)+"' or year(docDate)='"+(cal.get(Calendar.YEAR)-543)+"'  " +
//		"or year(docDate)='"+(cal.get(Calendar.YEAR)-1)+"' or year(docDate)='"+(cal.get(Calendar.YEAR)-543-1)+"' )";		
		
		BCAPInvoiceDAO dao = new BCAPInvoiceDAO();				
		List result = dao.find(hql);
//		System.out.println(hql);
		
		String docNo = "";
		String preDocNo = "";
		String maxDocNo = "";
//		NumberFormat formatter = new DecimalFormat("0000");
		if(result.get(0) != null) {
			
			maxDocNo = result.get(0).toString();
			
			if(dyeChem==true) {
				preDocNo = maxDocNo.substring(0, maxDocNo.length()-3);
				int temp = 1+Integer.valueOf(maxDocNo.substring(maxDocNo.length()-3,maxDocNo.length()));
				NumberFormat formatter = new DecimalFormat("000");
				docNo = formatter.format(temp);
			}else {
				preDocNo = maxDocNo.substring(0, maxDocNo.length()-4);
				int temp = 1+Integer.valueOf(maxDocNo.substring(maxDocNo.length()-4,maxDocNo.length()));
				NumberFormat formatter = new DecimalFormat("0000");
				docNo = formatter.format(temp);
			}
			
			
			maxDocNo = preDocNo+docNo; 	
			
		}else {
			if(dyeChem==true) {
				maxDocNo = "PO1-DC-"+docDateStr2[0].substring(2, 4)+docDateStr2[1]+"-001";
			}else {
				int yyyy = Integer.parseInt(docDateStr2[0]);
				if(yyyy<2500) {
					yyyy += 543; 
				}
				docDateStr2[0] = ""+yyyy;
				maxDocNo = "PO1-"+docDateStr2[0].substring(2, 4)+docDateStr2[1]+"-0001";
			}
		}
		bean.setDocNo(maxDocNo);
//		if(result!=null && result.size()>0) {
//			Object[] i = (Object[])result.get(0);			
//			bean.setDocNo(i[0].toString());
//
//		}
		return bean;
	}	
}
